#!/bin/sh
#(c) Copyright Barry Kauler 2009, puppylinux.com
#this script finds all builtin packages in Puppy Linux.
#110821 completely rewritten.
#111204 do not use /var/local/woof, instead 'status' directory (better for running from different save-files).
#121028 exclude pkgs that go into devx only from woof-installed-packages.
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14

export LANG=C

. ./DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.
if [ -f ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then #w478
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} #has FALLBACKS_COMPAT_VERSIONS
else
. ./DISTRO_PKGS_SPECS-${DISTRO_BINARY_COMPAT} #has FALLBACKS_COMPAT_VERSIONS
fi

echo "Creating file woof-installed-packages..."

#remove comments from PKGS_SPECS_TABLE... make sure '|' on end... get rid of old |+udev,+whatever on end...
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#' | grep -v '^$' | tr '\t' ' ' | tr -s ' ' | tr '+' '&' | sed -e 's%|&.*%%' | tr '&' '+' | sed -e 's% #.*%%' -e 's% $%%' -e 's%$%|%' -e 's%||$%|%'`"

#new script to find all pkgs for build...
./support/findpkgs
#...returns file status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
# each line is full db entry for pkg, prefixed with :generic-name:, pet/compat and repo-filename.
# ex: :a52dec:|compat|Packages-puppy-wary5-official|a52dec-0.7.4-w5|a52dec|0.7.4-w5||BuildingBlock|68K||a52dec-0.7.4-w5.pet||A free ATSC A52 stream decoder|puppy|wary5||
if [ $? -ne 0 ];then
 echo
 echo "ERROR: Script support/findpkgs aborted with an error, exiting."
 exit 1
fi
if [ ! -f status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} ];then
 echo
 echo "ERROR: Something went wrong with support/findpkgs, exiting."
 exit 1
fi

#need to find exactly what has gone into the build and the devx...
echo -n "" > /tmp/woof-installed-packages-tmp
echo -n "" > /tmp/devx-only-installed-packages-tmp #121028
cat status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} |
while read ONELINE
do
 #ex: :a52dec:|compat|Packages-puppy-wary5-official|a52dec-0.7.4-w5|a52dec|0.7.4-w5||BuildingBlock|68K||a52dec-0.7.4-w5.pet||A free ATSC A52 stream decoder|puppy|wary5||
 ADBENTRY="`echo -n "$ONELINE" | cut -f 4-19 -d '|'`"
 GENERICNAMES="`echo -n "$ONELINE" | cut -f 1 -d '|' | tr ':' ' '`"
 NAMEONLY="`echo -n "$ADBENTRY" | cut -f 2 -d '|'`"
 case $NAMEONLY in
  *_DEV)
   PTN1='^dev$'
   PTN2='dev>exe$'
  ;;
  *_DOC)
   PTN1='^doc>exe$'
   PTN2='^doc>dev$'
  ;;
  *_NLS)
   PTN1='^nls>exe$'
   PTN2='^nls>dev$'
  ;;
  *) #allow all thru...
   PTN1=''
   PTN2=''
  ;;
 esac
 PTN3='exe>dev$' #121028
 for AGENERICNAME in $GENERICNAMES
 do
  gnPTN="^yes|${AGENERICNAME}|"
  SPLITUPS="`echo "$PKGS_SPECS_TABLE" | grep "$gnPTN" | cut -f 4 -d '|' | tr ',' '\n'`"
  FLG3="`echo "$SPLITUPS" | grep "$PTN3"`" #121028
  if [ "$FLG3" ];then #121028
   echo "$ADBENTRY" >> /tmp/devx-only-installed-packages-tmp
   break
  fi
  FLG1="`echo "$SPLITUPS" | grep "$PTN1"`"
  FLG2="`echo "$SPLITUPS" | grep "$PTN2"`"
  sumFLGS="${FLG1}${FLG2}"
  if [ "$sumFLGS" ];then
   echo "$ADBENTRY" >> /tmp/woof-installed-packages-tmp
   break
  fi
 done
done
sync

#grep '|pet|' status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | cut -f 4-19 -d '|' > /tmp/woof-installed-packages-pet
#sync
#grep '|compat|' status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | cut -f 4-19 -d '|' > /tmp/woof-installed-packages-compat
#sync
#
##sort alphabetically...
#sort --key=1 --field-separator="|" /tmp/woof-installed-packages-pet > /tmp/petget_woof-installed-packages
#mv -f /tmp/petget_woof-installed-packages /tmp/woof-installed-packages-pet
#sort --key=1 --field-separator="|" /tmp/woof-installed-packages-compat > /tmp/petget_woof-installed-packages
#mv -f /tmp/petget_woof-installed-packages /tmp/woof-installed-packages-compat

sort --key=1 --field-separator="|" /tmp/woof-installed-packages-tmp > woof-installed-packages
if [ -s /tmp/devx-only-installed-packages-tmp ];then
 sort --key=1 --field-separator="|" /tmp/devx-only-installed-packages-tmp > devx-only-installed-packages
fi

###END###
